<?php

namespace common\models\myhtsq;

use common\models\BaseModel;
use common\repositories\myhtsq\ContractManageRepository;
use common\repositories\myhtsq\ErpUserRepository;
use common\services\myhtsq\ErpInterfaceService;
use common\support\StringHelper;

/**
 * 合同文档信息model
 * @author wangwx 2015-05-18
 */
class ContractDocumentModel extends BaseModel {
	
	 public $document_id;//文档ID
	 public $contract_id;//合同ID
	 public $pigeonhole_date;//文档归档日期
	 public $document_name;//文档名称
	 public $keeper;//保管人
	 public $contract_document_type;//纸质合同类型
	 public $document_ext_name;//文档类型
	 public $document_path;//存放路径
	 public $document_dir;//所属文件夹名称
	 public $created_on;//创建时间
	 public $created_by;//创建人
	 public $modified_on;//修改时间
	 public $modified_by;//修改人
	 
	 public function __construct($config = array()) {
        parent::__construct($config);
	 }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
        	//新增合同文档信息
            [['pigeonhole_date', 'created_on', 'modified_on'], 'safe','on'=>'add'],
            [['document_id', 'contract_id', 'keeper', 'created_by', 'modified_by'], 'string', 'max' => 36,'on'=>'add'],
            [['document_name'], 'string', 'max' => 100,'on'=>'add'],
            [['contract_document_type'], 'string', 'max' => 30,'on'=>'add'],
            [['document_ext_name'], 'string', 'max' => 10,'on'=>'add'],
            [['document_path'], 'string', 'max' => 500,'on'=>'add'],
            [['document_dir'], 'string', 'max' => 150,'on'=>'add'],
        		
        	//新增合同文档信息
        	[['document_id', 'contract_id', 'keeper'], 'required','on'=>'update'],
        	[['pigeonhole_date', 'created_on', 'modified_on'], 'safe','on'=>'update'],
        	[['document_id', 'contract_id', 'keeper', 'created_by', 'modified_by'], 'string', 'max' => 36,'on'=>'update'],
        	[['document_name'], 'string', 'max' => 100,'on'=>'update'],
        	[['contract_document_type'], 'string', 'max' => 30,'on'=>'update'],
        	[['document_ext_name'], 'string', 'max' => 10,'on'=>'update'],
        	[['document_path'], 'string', 'max' => 500,'on'=>'update'],
        	[['document_dir'], 'string', 'max' => 150,'on'=>'update']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'document_id' => '文档ID',
            'contract_id' => '合同ID',
            'pigeonhole_date' => '文档归档日期',
            'document_name' => '文档名称',
            'keeper' => '保管人',
            'contract_document_type' => '纸质合同类型',
            'document_ext_name' => '文档类型',
            'document_path' => '存放路径',
            'document_dir' => '所属文件夹名称',
            'created_on' => '创建时间',
            'created_by' => '创建人',
            'modified_on' => '修改时间',
            'modified_by' => '修改人',
        ];
    }

    /**
     * 处理导入前的数据
     * @param $rowData
     * @return array
     */
    public function processImportData($rowData)
    {
        $result = [
            'document_id' => StringHelper::uuid(),  //文档id
            'pigeonhole_date' => $rowData[9],   //纸质合同归档日期
            'keeper' => ErpUserRepository::getUserGuidByUserName($rowData[11]),  //保管人id
            'document_ext_name' => $rowData[7], //文件类型
            'document_type' => $rowData[8], //文档类型
            'contract_document_type' => '云客租赁合同',
            'document_path' => $rowData[10], //纸质合同存放位置
            'document_dir' => $rowData[12],  //纸质合同存放位置
            'created_on' => date('Y-m-d H:i:s'),
            'modified_on' => date('Y-m-d H:i:s'),
            'contract_id' => ContractManageRepository::getContractIdByContractNum(trim($rowData[4])),
        ];
        return $result;
    }

}
